package com.xr.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Utils {

  private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

  public static Map<String, Object> newHashMap(Object... params) {
    Map<String, Object> map = new HashMap<String, Object>();
    for (int i = 0; i < params.length; i += 2) {
      map.put(params[i].toString(), params[i + 1]);
    }
    return map;
  }

  /**
   * Md5 加密工具
   *
   * @param content String   not null
   * @return 加密后的 MD5 值 32位大写的
   */
  public static String md5(String content) throws Exception {
    Assert.hasLength(content, "encryption content is empty.");

    byte[] btInput = content.getBytes();
    // 获得MD5摘要算法的 MessageDigest 对象
    MessageDigest mdInst = MessageDigest.getInstance("MD5");
    // 使用指定的字节更新摘要
    mdInst.update(btInput);
    // 获得密文
    byte[] md = mdInst.digest();
    // 把密文转换成十六进制的字符串形式
    int j = md.length;
    char str[] = new char[j * 2];
    int k = 0;
    for (int i = 0; i < j; i++) {
      byte byte0 = md[i];
      str[k++] = hexDigits[byte0 >>> 4 & 0xf];
      str[k++] = hexDigits[byte0 & 0xf];
    }
    return new String(str);
  }

  /**
   * 生成随机数
   *
   * @param type 你要几位的验证码 默认可以使用 2-8 位的验证码，type 非法，直接返回6位
   * @return 随机数
   */
  public static int randomNumber(int type) {
    if (type == 8) {
      return (int) ((Math.random() * 9 + 1) * 10000000);
    }
    if (type == 7) {
      return (int) ((Math.random() * 9 + 1) * 1000000);
    }
    if (type == 6) {
      return (int) ((Math.random() * 9 + 1) * 100000);
    }
    if (type == 5) {
      return (int) ((Math.random() * 9 + 1) * 10000);
    }
    if (type == 4) {
      return (int) ((Math.random() * 9 + 1) * 1000);
    }
    if (type == 3) {
      return (int) ((Math.random() * 9 + 1) * 100);
    }
    if (type == 2) {
      return (int) ((Math.random() * 9 + 1) * 10);
    }
    return (int) ((Math.random() * 9 + 1) * 100000);
  }

  /**
   * 生成字母+数字（8位）随机数
   *
   * @param length 长度
   * @return val 转换好的文本
   */
  public static String getStringRandom(int length) {
    String val = "";
    Random random = new Random();
    //length为几位密码
    for (int i = 0; i < length; i++) {
      String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
      //输出字母还是数字
      if ("char".equalsIgnoreCase(charOrNum)) {
        //输出是大写字母还是小写字母
        int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
        val += (char) (random.nextInt(26) + temp);
      } else if ("num".equalsIgnoreCase(charOrNum)) {
        val += String.valueOf(random.nextInt(10));
      }
    }
    return val;
  }

  public static BufferedImage drawImage(String content) {

    Assert.hasLength(content, "无法生成图片，请检查内容是否正确。");

    int width = 85, height = 20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    Random random = new Random();
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
      int x = random.nextInt(width);
      int y = random.nextInt(height);
      int xl = random.nextInt(12);
      int yl = random.nextInt(12);
      g.drawLine(x, y, x + xl, y + yl);
    }

    for (int i = 0;i<content.length();i++) {
      g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
      g.drawString(String.valueOf(content.charAt(i)), 13 * i + 6, 16);
    }

    g.dispose();
    return image;
  }

  /**
   *
   * @param fc
   * @param bc
   * @return
   */
  private static Color getRandColor(int fc, int bc) {
    Random random = new Random();
    if (fc > 255){
      fc = 255;
    }
    if (bc > 255){
      bc = 255;
    }
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);
    return new Color(r, g, b);
  }
}
